/*
 * Copyright 2009 Jesse McLaughlin (nzjess@gmail.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.travelfusion.xmlclient.handler.xmltool;

import static org.travelfusion.xmlclient.util.TfXAPIUtil.hasText;

import java.io.InputStream;

import org.travelfusion.xmlclient.exception.TfXClientException;
import org.travelfusion.xmlclient.handler.RequestHandler;
import org.travelfusion.xmlclient.handler.RequestHandlerInterceptor;
import org.travelfusion.xmlclient.handler.ResponseHandler;
import org.travelfusion.xmlclient.handler.ResponseHandlerInterceptor;
import org.travelfusion.xmlclient.login.Login;
import org.travelfusion.xmlclient.xobject.XRequest;
import org.travelfusion.xmlclient.xobject.XResponse;

import com.google.inject.Inject;
import com.mycila.xmltool.XMLDoc;
import com.mycila.xmltool.XMLTag;

/**
 * @author Jesse McLaughlin (nzjess@gmail.com)
 */
public class XmlToolHandlerInterceptor implements RequestHandlerInterceptor, ResponseHandlerInterceptor {

  private XmlToolTemplateCache templateCache;

  private XMLTag template;

  @Inject
  public void setTemplateCache(XmlToolTemplateCache templateCache) {
    this.templateCache = templateCache;
  }

  public void beforeRequest(Login login, RequestHandler<XRequest> handler, XRequest request) {
    template = templateCache.getTemplate(handler.getClass(), request.getClass());
    doLogin(login, handler, request);
    template.gotoRoot();
    ((XmlToolRequestHandler<XRequest>)handler).setTemplate(template);
  }

  public void afterRequest(RequestHandler<XRequest> handler, XRequest request) {
  // nop
  }

  protected void doLogin(Login login, RequestHandler<XRequest> handler, XRequest request) {
    setLoginIdOnTag((login != null) ? login.getXmlLoginId() : null, "XmlLoginId");
    setLoginIdOnTag((login != null) ? login.getLoginId() : null, "LoginId");
  }

  protected void setLoginIdOnTag(String loginId, String loginTagName) {
    if (template.gotoRoot().gotoChild().hasTag(loginTagName) && !hasText(template.gotoChild(loginTagName).getText())) {
      if (hasText(loginId)) {
        template.addText(loginId);
      }
      else {
        throw new TfXClientException("missing login id for element: " + loginTagName);
      }
    }
  }

  public void beforeResponse(ResponseHandler<XRequest, XResponse> handler, InputStream responseStream) {
    XMLTag responseTag = XMLDoc.from(responseStream, true);
    ((XmlToolResponseHandler<XRequest, XResponse>)handler).setResponseTag(responseTag);
  }

  public void afterResponse(ResponseHandler<XRequest, XResponse> handler, XResponse response) {
  // nop
  }
}
